這裡分享第二個策略 - 菲阿裡四價策略
菲阿裡四價指的是:昨日高點、昨日低點、昨天收盤、今天開盤四個價格。
菲阿裡四價上下軌的計算非常簡單。昨日高點為上軌,昨日低點為下軌。當價格突破上軌時,買入開倉;當價格突破下軌時,賣出開倉。
作法
第一步:獲取昨日最高價、最低價、收盤價、開盤價四個數據。
第二步:計算上軌和下軌。當價格上穿上軌時,買入開倉;當價格下穿下軌時,賣出開倉。
第三步:當日平倉。
from shioaji.data import Kbars
kbars = api.kbars(api.Contracts.Futures.TXF['TXF202110'], start="2021-09-29", end="2021-09-29")
df = pd.DataFrame({**kbars})
df.ts = pd.to_datetime(df.ts)
nwedf = df.drop(df[(df['ts'] < "2021-09-29 08:45:00") | (df['ts'] > "2021-09-29 13:45:00")].index).reset_index()
open = nwedf['Open'][0]
close = nwedf['Close'][0]
column = nwedf['High']
max_value = column.max()
column = nwedf['Low']
min_value = column.min()
minute_close = pd.Series()
stock = 0
price = 0
reward = 0
sig = []
for i in range(0,700):
# 抓snapshot
snapshots = api.snapshots(contracts)
# 存到分k收盤價的series
minute_close = minute_close.append(pd.Series(
[snapshots[0].close],
index=[pd.to_datetime(snapshots[0].ts, unit='ns')]
))
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
# 當價格上穿上軌時,買入開倉
if snapshots[0].close > max_value and stock == 0:
stock += 1
price = snapshots[0].close
sig.append(1)
print("Current Time =", current_time, "buy :", price)
# 當價格下穿下軌時,賣出開倉
elif snapshots[0].close < min_value and stock == 1:
stock -= 1
reward = price - snapshots[0].close
sig.append(-1)
print("Current Time =", current_time, "sell :", price, "reward :", reward)
else:
sig.append(0)
time.sleep(60)
# 當日平倉
if stock > 0:
reward = price - snapshots[0].close
sig.append(-1)
print("Current Time =", current_time, "sell :", price, "reward :", reward)
elif stock < 0:
reward = price - snapshots[0].close
sig.append(-1)
print("Current Time =", current_time, "buy :", price, "reward :", reward)
實際跑了一下策略,發現這個策略適合用在多頭市場,假如市場轉空,應該把策略反過來操作